
<?php
	include("DBConn.php");
	$item_name = $_GET['item'];
	$quantity = $_POST['quantity'];
	$taker = $_POST['taker'];
	$purpose = $_POST['purpose'];
?>
<html>
<body>
<?php
	$link = connectToDB();
	$qtyquery = "select QUANTITY from item_status where ITEM_NAME='$item_name'";
	$resultqty = mysql_query($qtyquery) or die(mysql_error());
	$currqty = mysql_fetch_row($resultqty);
	$updatedqty = $currqty[0]-$quantity;
	
	
	$strQuery0 = "update item_status set QUANTITY='$updatedqty', RECENT_QTY_TAKEN='$quantity', TAKER='$taker', DATE_LAST_TAKEN=CURRENT_TIMESTAMP where ITEM_NAME='$item_name';";
	$addQuery = "insert into items_taken values ('$item_name', '$quantity', CURRENT_TIMESTAMP, '$taker', '$purpose');";
	
	$update = mysql_query($strQuery0) or die(mysql_error());
	$add = mysql_query($addQuery) or die(mysql_error());
	
	
	$strQuery = "select ITEM_NAME, QUANTITY, RECENT_QTY_TAKEN, TAKER, DATE_LAST_TAKEN from item_status where ITEM_NAME='$item_name'";
	$result = mysql_query($strQuery) or die(mysql_error());
	
	if($update){
		echo "<h2>Successfully taken!</h2>";
	}
	/*
	if(mysql_fetch_row($result)==0){
		echo "<h2>Item not found!</h2>";
	}
	else{
	*/?>
		<center>
		<table border="1" cellspacing="0" width="80%">
			<tr>
			<?php
			
			while ($field=mysql_fetch_field($result)) {
			echo "<th>";
			echo "<center>$field->name</center>";
			echo "</th>";
			}
			echo "</tr>";

			while ($row = mysql_fetch_row($result)) {
			echo "<tr>";
			for ($i=0; $i<mysql_num_fields($result); $i++) {
			echo "<td>";
			if($row[$i]!='0000-00-00 00:00:00')
				echo "<center>$row[$i]</center>";
			echo "</td>";
			}
			echo "</tr>\n";
			}

		echo "</table><p /><a href='add_itemqty.php?item=$item_name' target='parameters'>Add to Quantity</a> | <a href='take_itemqty.php?item=$item_name' target='parameters'>Take Some</a></center>";
	//}
	mysql_close($link);
?>
</body>
</html>